package com.zsx.shopping.order.controller;

import com.zsx.shopping.common.api.R;
import com.zsx.shopping.common.exception.ShoppingException;
import com.zsx.shopping.order.annotation.OrderPermissionCheck;
import com.zsx.shopping.order.enums.DeliveryType;
import com.zsx.shopping.order.enums.PaymentType;
import com.zsx.shopping.order.form.ConfirmForm;
import com.zsx.shopping.order.service.OrderService;
import com.zsx.shopping.order.vo.PayStatusVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/order/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @PostMapping("/submit")
    public R<String> submitOrder(@Valid @RequestBody ConfirmForm confirmForm) {
        if (DeliveryType.valueOf(confirmForm.getDeliveryType()) == null || PaymentType.valueOf(confirmForm.getPaymentType()) == null)
            throw new ShoppingException();
        return R.success(orderService.submitOrder(confirmForm));
    }

    @PostMapping("/toTrade")
    public R<String> toTrade() {
        return R.success(orderService.toTrade());
    }

    @GetMapping("/orderStatus")
    public R<Byte> getOrderStatus(@RequestParam Long orderId) {
        return R.success(orderService.getOrderStatus(orderId));
    }

    @OrderPermissionCheck(sn = "orderSn")
    @GetMapping("/payStatus")
    public R<PayStatusVO> getPayStatus(@RequestParam String orderSn) {
        return R.success(orderService.getPayStatus(orderSn));
    }

    @OrderPermissionCheck(sn = "orderSn")
    @PostMapping("/pay")
    public R<PayStatusVO> pay(@RequestParam String orderSn) {
        orderService.pay(orderSn);
        return R.success(orderService.getPayStatus(orderSn));
    }

    @OrderPermissionCheck(sn = "orderSn")
    @PutMapping("/cancel")
    public R<Void> cancelOrder(@RequestParam String orderSn) {
        orderService.cancelOrder(orderSn);
        return R.success();
    }
}
